# -*- coding: utf-8 -*-
from typing import Optional
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from curl_cffi import requests
from feapder.db.redisdb import RedisDB
from feapder.network.user_pool import GuestUser
from feapder.network.user_pool import GuestUserPool
import util.MyUtils as U


class UserPool(GuestUserPool):
    def __init__(self, redis_key, **kwargs):
        super().__init__(redis_key, **kwargs)
        self._redisdb = RedisDB(
            ip_ports="139.9.196.26:6379",
            # ip_ports="127.0.0.1:6379",
            user_pass='python',
            db=9,
        )


class TyGuestUserPool(UserPool):
    url = "https://www.tianyancha.com/login"

    def login(self) -> Optional[GuestUser]:
        res: requests.Response = requests.get(
            self.url, proxies=U.get_proxy(), impersonate=requests.BrowserType.chrome110, timeout=5)
        tyc_id = res.cookies.get('TYCID')
        user = GuestUser(
            user_agent=tyc_id,
            cookies=dict(res.cookies),
            tyc_id=tyc_id
        )
        return user


user_pool = TyGuestUserPool(
    "getPhone:user:pool",
    min_users=5000,
    keep_alive=True,
)

if __name__ == '__main__':
    user_pool.run()


    # users = user_pool.get_user()
    # print("取到user：", users)
    # print("cookie：", users.cookies)
    # print("user_agent：", users.user_agent)
    # print("proxies：", users.proxies)
